package com.huilian.iotbox.admin.wap.controller;

import com.huilian.iotbox.admin.wap.service.UserService;
import com.huilian.iotbox.data.common.ServerResponse;
import com.huilian.iotbox.data.controller.BaseController;
import com.huilian.iotbox.data.dto.UserDto;
import com.huilian.iotbox.data.po.User;
import com.huilian.iotbox.data.properties.ConfigProperties;
import com.huilian.iotbox.data.properties.SmsProperties;
import com.huilian.iotbox.data.utils.Util;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @program: huilian-iotbox
 * @description: LoginController
 * @author: zhangyingjie
 * @create: 2021-09-06 20:04
 */
@Controller
@RequestMapping("/api/login")
@Slf4j
public class LoginController extends BaseController {

    @Autowired
    private UserService userService;
    @Autowired
    private SmsProperties smsProperties;
    @Autowired
    public ConfigProperties configProperties;

    @RequestMapping("/getLogin")
    public String getLoginPage(HttpServletRequest request){
        log.info(Util.getIpAddress(request));
//        return "redirect:http://127.0.0.1:9904/wap/#/pages/index/index";
        StringBuilder url = new StringBuilder("redirect:")
                .append(configProperties.getWapBackEndUrl())
                .append("/#/pages/index/index");
        System.out.println("我进来了，我要去" + url);
        return String.valueOf(url);
    }

    @ResponseBody
    @PostMapping
    public ServerResponse doLogin(@RequestBody User param) {
        try {
            return userService.login(param);
        } catch (Exception e) {
            log.error("登录异常：{}", e);
            return ServerResponse.createByErrorMessage("账号或密码不存在");
        }
    }

    @ResponseBody
    @PostMapping("/smsCode")
    public ServerResponse smsCode(@RequestBody UserDto userDto, HttpServletRequest request) {
        return userService.smsCode(userDto, smsProperties.getKey());
    }
}
